<template>
  <div class="photo">
    <!-- eslint-disable prettier/prettier  -->
    <!-- 上传证件照 -->
    <div class="upload-card">
      <div class="upload-card-title">
        <span>大一寸蓝底</span>证件照（电子版）
      </div>
      <van-uploader
        class="upload"
        :after-read="file => uploadImg(file, 'papers_image')"
      >
        <div class="IDcard-front">
          <img class="IDcard-front-img" :src="imgs.papers_image" />
          <img
            class="upload-button"
            v-if="
              !imgs.papers_image || imgs.papers_image.indexOf('http') === -1
            "
            src="@/static/add.png"
          />
        </div>
      </van-uploader>
    </div>
    <div class="upload-card">
      <div class="upload-card-title"><span>白底</span>半身照</div>
      <van-uploader
        class="upload"
        :after-read="file => uploadImg(file, 'person_image')"
      >
        <div class="IDcard-front">
          <img class="IDcard-front-img" :src="imgs.person_image" />
          <img
            class="upload-button"
            v-if="
              !imgs.person_image || imgs.person_image.indexOf('http') === -1
            "
            src="@/static/add.png"
          />
        </div>
      </van-uploader>
    </div>
    <div class="example">
      <div class="example-title">证件上传示例</div>
      <div class="example-wrapper">
        <img src="@/static/manhead@2x.png" />
        <img src="@/static/icon_photo@2x.png" />
        <div>证件照格式可为png/jpg/jpeg。</div>
        <div>请按照要求上传正式的证件照。</div>
      </div>
    </div>
    <button
      bindtap="goNext"
      :class="`button ${
        imgs.papers_image && imgs.person_image ? 'green-button' : ''
      }`"
      size="mini"
    >
      提交
    </button>
    <div v-if="time" class="mask"></div>
    <div v-if="time" class="model">
      <div class="model-title">错误示例</div>
      <div class="model-desc">请勿上传生活照/他拍照。</div>
      <div class="time">{{ time }} s</div>
      <div class="model-item-wrapper">
        <img class="model-item" src="@/static/images1@2x.png" />
        <img class="model-item" src="@/static/images2@2x.png" />
        <img class="model-item" src="@/static/images3@2x.png" />
        <img class="model-item" src="@/static/images4@2x.png" />
      </div>
    </div>
    <div class="nav" @click="$router.go(-1)">返回</div>
  </div>
</template>

<script>
import papers_image from "@/static/upload_photo@2x.png";
import person_image from "@/static/upload_photo@2x.png";
export default {
  data() {
    return {
      imgs: {
        papers_image,
        person_image
      },
      setIntervalId: null,
      time: 3,
      fileList: []
    };
  },
  mounted() {
    const setIntervalId = setInterval(() => {
      this.time--;

      if (this.time <= 0) {
        clearInterval(setIntervalId);
        this.setIntervalId = null;
      }
    }, 1000);
    this.setIntervalId = setIntervalId;
  },
  methods: {
    /**
     * @author 黎明霞光 2021-05-18
     * 读取完图片后
     * file 选取的文件对象
     * name 上传的图片的字段名称
     **/
    uploadImg(file, name) {
      console.log(file);
      // this.fileList = [{ ...file }];
      // TODO: 请求接口后赋值
      // 下面的是写死的
      this.imgs[name] = "https://www.baidu.com/img/flexible/logo/pc/result.png";
    },
    /**
     * @author 鲁佳明 2021-05-18
     * 提交保存
     */
    goNext() {
      // TODO: 提交保存
    }
  },
  beforeDestroy() {
    clearInterval(this.setIntervalId);
    this.setIntervalId = null;
  }
};
</script>

<style lang="less" scoped>
.photo {
  background-color: #f1f1f1;
  box-sizing: border-box;
  padding-bottom: 0.4rem;
  overflow: hidden;
}
.upload-card {
  width: 100%;
  height: 3.98rem;
  background: #ffffff;
  margin-top: 0.17rem;
  overflow: hidden;
}
.upload {
  display: block;
  width: 2.05rem;
  margin: 0.3rem auto 0.42rem;
}
.upload-card .IDcard-front {
  display: block;
  position: relative;
  width: 2.05rem;
  height: 2.45rem;
  margin: 0.3rem auto 0.42rem;
}

.IDcard-front .IDcard-front-img {
  display: block;
  width: 100%;
  height: 100%;
}

.upload-card .upload-card-title {
  font-size: 0.24rem;
  color: #666666;
  margin-left: 0.41rem;
  margin-top: 0.2rem;
}
.upload-card .upload-card-title span {
  color: #73a1ff;
}
.upload-card .upload-button {
  box-sizing: border-box;
  width: 1.2rem;
  height: 1.2rem;
  background: #73a1ff;
  border-radius: 50%;
  padding: 0.2rem;
  position: absolute;
  left: calc(50% - 0.6rem);
  top: calc(50% - 0.6rem);
}

.example .example-title {
  font-size: 0.28rem;
  font-weight: bold;
  color: #73a1ff;
  margin-top: 0.32rem;
  margin-left: 0.32rem;
  margin-bottom: 0.23rem;
}

.example-wrapper img {
  width: 1.04rem;
  height: 1.19rem;
  background-color: #fff;
}

.example-wrapper img:first-child {
  margin-left: 0.32rem;
  margin-right: 0.21rem;
}

.example-wrapper div {
  margin-left: 0.32rem;
  font-size: 0.22rem;
  font-weight: bold;
  color: #666666;
}

.button {
  display: block;
  width: 3rem;
  height: 0.64rem;
  margin: 0.5rem auto 0.5rem;
  border-radius: 0.1rem;
  font-size: 0.28rem;
  border: none !important;
  background: #dddddd;
  color: #999999;
}

.green-button {
  background: #07c160;
  color: #fff;
}

.mask {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 100%;
  background: #000000;
  opacity: 0.5;
  z-index: 98;
}

.model {
  position: fixed;
  top: 1.1rem;
  left: calc(50% - 3.43rem);
  width: 6.86rem;
  height: 9.21rem;
  background: #f1f1f1;
  z-index: 99;
}

.model .model-title {
  font-size: 0.32rem;
  font-weight: bold;
  color: #73a1ff;
  text-align: center;
  margin-top: 0.2rem;
  /* margin-bottom: 40rem; */
}
.model .model-desc {
  font-size: 0.24rem;
  font-weight: bold;
  color: #666666;
  text-align: center;
  margin-top: 0.1rem;
  margin-bottom: 0.6rem;
}
.model .time {
  position: absolute;
  top: 0.2rem;
  right: 0.5rem;
  color: #666;
  font-size: 0.32rem;
}

.model-item-wrapper {
  width: 5.66rem;
  margin-left: auto;
  margin-right: auto;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

.model-item {
  width: 2.52rem;
  height: 3.35rem;
  margin-bottom: 0.3rem;
}
.nav {
  position: fixed;
  bottom: 20%;
  right: 0;
  border-radius: 0.15rem 0 0 0.15rem;
  width: 1rem;
  height: 1rem;
  line-height: 1rem;
  text-align: center;
  background-color: #78a4ff;
  color: #fff;
  font-size: 0.26rem;
}
</style>
